package com.shd.edanpei.ui.adapter;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseQuickImageAdapter;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.bean.OrderListModel;
import com.shd.edanpei.enums.StatusEnum;
import com.shd.edanpei.enums.OrderTypeEnum;

/**
 * Created by mc on 2018/9/19.
 * yang-wen-wen
 */

public class OrderAdapter extends BaseQuickImageAdapter<OrderListModel> {

    private static int mOrder1Type;
    private View cancel, unSure, sure, remove, goCreate, tv_first, tv_end, tv_updata,payService;

    public OrderAdapter(Context context, int type) {
        super(context, R.layout.item_order2);
        mOrder1Type = type;
    }

    @Override
    protected void convert(BaseViewHolder helper, OrderListModel item) {

        ImageView ivType = helper.getView(R.id.iv_type);
        TextView tvType = helper.getView(R.id.tv_type);
        if (item.getOrderType() == OrderTypeEnum.car.getKey()) {
            ivType.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_label_car));
            tvType.setText(OrderTypeEnum.car.getValue());
        } else if (item.getOrderType() == OrderTypeEnum.house.getKey()) {
            ivType.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_label_house));
            tvType.setText(OrderTypeEnum.house.getValue());
        } else if (item.getOrderType() == OrderTypeEnum.normal.getKey()) {
            ivType.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.ic_label_common));
            tvType.setText(OrderTypeEnum.normal.getValue());
        } else if (item.getOrderType() == OrderTypeEnum.personal.getKey()) {
            ivType.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.xinyong));
            tvType.setText(OrderTypeEnum.personal.getValue());
        } else if (item.getOrderType() == OrderTypeEnum.enterprise.getKey()) {
            ivType.setImageDrawable(ContextCompat.getDrawable(mContext, R.drawable.qiye));
            tvType.setText(OrderTypeEnum.enterprise.getValue());
        }

        helper.setText(R.id.tv_name, item.getCustomerName())
                .setText(R.id.tv_address, item.getArea())
                .setText(R.id.tv_time, item.getCreateTime())
                .addOnClickListener(R.id.tv_first)
                .addOnClickListener(R.id.tv_end)
                .addOnClickListener(R.id.tv_cancel)
                .addOnClickListener(R.id.tv_sure)
                .addOnClickListener(R.id.tv_remove)
                .addOnClickListener(R.id.tv_goCreate)
                .addOnClickListener(R.id.tv_updata)
                .addOnClickListener(R.id.tv_service);


        if (mOrder1Type == Constant.ME_ORDER) {
            //我的订单
            initMeButton(helper, item.getUserStatus(), item.getManagerId());
            helper.setText(R.id.tv_status, StatusEnum.getUserDes(item.getUserStatus()));
        } else if (mOrder1Type == Constant.SHARE_ORDER) {
            //共享订单
            initShareButton(helper, item.getManagerStatus());
            helper.setText(R.id.tv_status, StatusEnum.getManagerDes(item.getManagerStatus()));
        } else if (mOrder1Type == 2) {
            //普通订单
            initNormalButton(helper);
            helper.setText(R.id.tv_status, "");
        }
    }

    private void initMeButton(BaseViewHolder helper, int status, int managerID) {

        //撤销
        cancel = helper.getView(R.id.tv_cancel);
        cancel.setVisibility(View.GONE);
        //立即确认（不能操作)
        unSure = helper.getView(R.id.tv_unsure);
        unSure.setVisibility(View.GONE);
        //立即确认
        sure = helper.getView(R.id.tv_sure);
        sure.setVisibility(View.GONE);
        //移除
        remove = helper.getView(R.id.tv_remove);
        remove.setVisibility(View.GONE);
        //继续创建
        goCreate = helper.getView(R.id.tv_goCreate);
        goCreate.setVisibility(View.GONE);
        //初审
        tv_first = helper.getView(R.id.tv_first);
        tv_first.setVisibility(View.GONE);
        //终审
        tv_end = helper.getView(R.id.tv_end);
        tv_end.setVisibility(View.GONE);
        payService=helper.getView(R.id.tv_service);
        payService.setVisibility(View.GONE);
        //修改
        tv_updata = helper.getView(R.id.tv_updata);
        tv_updata.setVisibility(View.GONE);


        if (status == StatusEnum.unpaid.getStatus()) {
            cancel.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.applying.getStatus()) {
            cancel.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.first.getStatus()) {
            //已被接单，未初审
            cancel.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.last.getStatus()) {
            //已初审，未终审核
            cancel.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.waitService.getStatus()) {
            //已终审，未支付服务费
           payService.setVisibility(View.VISIBLE);
            cancel.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.waitLoan.getStatus()) {
            //未放款
            unSure.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.waitfirm.getStatus()) {
            //已放款
            sure.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.success.getStatus()) {
            //交易成功
        } else if (status == StatusEnum.error.getStatus()) {
            //订单超时，无人接单
            cancel.setVisibility(View.VISIBLE);
            goCreate.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.cancel.getStatus()) {
            //未支付，撤销订单
            remove.setVisibility(View.VISIBLE);
            tv_updata.setVisibility(View.VISIBLE);
//            tv_sureUpdate.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.refuse.getStatus()) {
            //被拒绝
            cancel.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.waitRepay.getStatus()) {
            //已支付，撤销订单,退款中
//            remove.setVisibility(View.VISIBLE);
            if (managerID > 0) {

            } else {
                tv_updata.setVisibility(View.VISIBLE);
//                tv_sureUpdate.setVisibility(View.VISIBLE);
            }
        } else if (status == StatusEnum.repaySuccess.getStatus()) {
            //退款成功
            remove.setVisibility(View.VISIBLE);
        }
    }


    private void initShareButton(BaseViewHolder helper, int status) {
        //撤销
        cancel = helper.getView(R.id.tv_cancel);
        payService=helper.getView(R.id.tv_service);
        //立即支付
        //立即确认（不能操作)
        unSure = helper.getView(R.id.tv_unsure);
        //立即确认
        sure = helper.getView(R.id.tv_sure);
        //移除
        remove = helper.getView(R.id.tv_remove);
        //继续创建
        goCreate = helper.getView(R.id.tv_goCreate);
        tv_first = helper.getView(R.id.tv_first);
        tv_end = helper.getView(R.id.tv_end);
        //修改
        tv_updata = helper.getView(R.id.tv_updata);
        tv_updata.setVisibility(View.GONE);

        cancel.setVisibility(View.GONE);
        payService.setVisibility(View.GONE);
        unSure.setVisibility(View.GONE);
        sure.setVisibility(View.GONE);
        remove.setVisibility(View.GONE);
        goCreate.setVisibility(View.GONE);
        tv_first.setVisibility(View.GONE);
        tv_end.setVisibility(View.GONE);

        if (status == StatusEnum.first.getStatus()) {
            //待初审
            cancel.setVisibility(View.VISIBLE);
            tv_first.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.last.getStatus()) {
            //待终审
            tv_end.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.waitService.getStatus()) {
            //已终审，未支付服务费
            unSure.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.waitLoan.getStatus()) {
            //未放款
            sure.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.waitfirm.getStatus()) {
            //已放款
           // sure.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.success.getStatus()) {
            //交易成功

        } else if (status == StatusEnum.error.getStatus()) {
            //订单超时，无人接单
//            cancel.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.cancel.getStatus()) {
            //未支付，撤销订单
            remove.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.refuse.getStatus()) {
            //被拒绝
//            cancel.setVisibility(View.VISIBLE);
            remove.setVisibility(View.VISIBLE);
        } else if (status == StatusEnum.waitRepay.getStatus()) {
            //已支付，撤销订单,退款中

        } else if (status == StatusEnum.repaySuccess.getStatus()) {
            //退款成功
//            remove.setVisibility(View.VISIBLE);
        }else if(status == StatusEnum.waitService.getStatus()){


        }

    }

    private void initNormalButton(BaseViewHolder helper) {
    }


    public static void setOrder1Type(int order1Type) {
        mOrder1Type = order1Type;
    }
}
